package com.example.module_filemanage.MVVM.Module.Business;

import com.example.module_filemanage.Interface.ICallBack;
import com.example.module_filemanage.MVVM.Module.Entil.Item_FileInfo_module;
import com.example.module_filemanage.MVVM.Module.Entil.Item_Left_Menu_Module;
import com.example.module_filemanage.MVVM.Module.Entil.Left_menu;
import com.example.module_filemanage.Public.Data;
import com.example.module_filemanage.Utils.TimeTool;
import com.example.module_filemanage.Utils.Tool;
import com.qiniu.storage.model.FileInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by SJ on 2018/4/20.
 */

public class TranModule {
    //空间信息数据
    public static Left_menu getitemleftmenumodule(){
        ArrayList<Item_Left_Menu_Module> items=new ArrayList<>();
        long size=0;
        for (String spacename : Data.getInstance().SPACES) {
            size+=Data.getInstance().SPACESIZE.get(spacename);
            Item_Left_Menu_Module item=new Item_Left_Menu_Module(spacename,Data.getInstance().SPACEFILECOUNT.get(spacename), Tool.getBytetoSize(Data.getInstance().SPACESIZE.get(spacename)),Data.getInstance().SPACESURL.get(spacename));
            items.add(item);
        }
        Left_menu _left_menu=new Left_menu(items,Tool.getBytetoSize(size),Tool.getprogress(size));
        return _left_menu;
    }
    //空间文件数据
    public static List<Item_FileInfo_module> item_fileinfo(List<FileInfo> items,String spacename){
        List<Item_FileInfo_module> _arrayList=new ArrayList<>();
       // Log.e(TAG, "item_fileinfo: "+spacename );
        String url=Data.getInstance().SPACESURL.get(spacename);
        for (int i=0; i < items.size(); i++) {
            FileInfo _fileInfo=items.get(i);
            Item_FileInfo_module item=new Item_FileInfo_module(_fileInfo.key,Tool.getBytetoSize(_fileInfo.fsize), TimeTool.Unixtotime(_fileInfo.putTime),
                    "http://"+url+"/"+_fileInfo.key,_fileInfo.key.matches(".+\\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)"),_fileInfo.key.substring(_fileInfo.key.lastIndexOf("-")+1,_fileInfo.key.lastIndexOf(".")),spacename);
           // Log.e(TAG, "item_fileinfo: "+"http://"+url+"/"+_fileInfo.key );
            _arrayList.add(item);
        }
        //排序

        return _arrayList;
    }
    //所有文件排序以及统计
    public static void allSort(final String[] account, final ICallBack callback){
        new Thread(new Runnable() {
            @Override
            public void run() {
                int[] count=new int[account.length+1];
                Collections.sort(Data.getInstance().ALLFILEINFO,new Comparator<Item_FileInfo_module>() {
                    @Override
                    public int compare(Item_FileInfo_module IT0, Item_FileInfo_module IT1) {
                        return -IT0.getFiletime().compareTo(IT1.getFiletime());
                    }
                });
                boolean other=true;
                for (Item_FileInfo_module file: Data.getInstance().ALLFILEINFO) {
                    other=true;
                    for (int i=0; i <account.length ; i++) {
                        if (file.getAccount().equals(account[i])){
                            count[i]++;
                            other=false;
                            break;
                        }
                    }
                    if (other){
                        count[account.length]++;
                    }
                }
                callback.success(count);
            }
        }).start();


    }

    public static String[] itemToArray(Item_FileInfo_module item){
        String[] titles=new String[3];
        titles[0]=item.filename;
        titles[1]=item.filesize+"    "+item.filetime;
        titles[2]=item.fileurl;
        return  titles;
    }

    public static boolean isaccount(String key){
        if (key.lastIndexOf("-")!=-1 && key.lastIndexOf(".")!=-1){
            return key.substring(key.lastIndexOf("-")+1,key.lastIndexOf(".")).equals(Data.account)?true:false;
        }
        return true;
    }
}
